//
//  MHSetPropertyOrTypeViewController.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/16.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSetPropertyOrTypeViewController.h"
#import "MHSetCirclePropertyViewModel.h"
#import "UIImage+LCActionSheet.h"
#import "MHH5Links.h"
#import "MHWebViewModel.h"

@interface MHSetPropertyOrTypeViewController ()
@property (nonatomic, readonly, strong) MHSetCirclePropertyViewModel *viewModel;
@property (weak, nonatomic) IBOutlet UIButton *checkButton;
@property (weak, nonatomic) IBOutlet UIButton *topButton;
@property (weak, nonatomic) IBOutlet UIButton *bottomBtn;
@property (weak, nonatomic) IBOutlet UILabel *metionLable;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property(nonatomic,strong,readwrite) YYLabel *protocolLable;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nextBtnBottomConstraint;
@property (weak, nonatomic) IBOutlet UILabel *tipsLabel;

@end

@implementation MHSetPropertyOrTypeViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self _setupNavigationItem];

    [self masLayoutSubViews];
    self.tipsLabel.font = MHRegularFont_12;
    self.nextBtn.titleLabel.font = MHRegularFont_15;
    self.nextBtn.enabled = false;
    self.checkButton.selected = YES;
    
    self.topButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.bottomBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    if(self.viewModel.isFinished)
    {
        self.bottomBtn.selected = YES;
        self.viewModel.info.payType  = ![AXAppDelegate sharedDelegate].isAuthorizationExpired ? MHCirclPayTypeFree :  MHCirclPayTypeCharge;
        self.nextBtn.enabled = YES;
    }
//    self.navigationController.navigationBar.translucent = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
//    [self.topButton setBackgroundImage:[[UIImage lc_imageWithColor:MHColor(236, 107, 49)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  forState:UIControlStateSelected];
//    if (self.viewModel.isFinished) {
//         [self.bottomBtn setBackgroundImage:[UIImage lc_imageWithColor:MHColor(236, 107, 49)]  forState:UIControlStateSelected];
//    }
   
    
//    [self.topButton setBackgroundImage:[[UIImage lc_imageWithColor:MHTopBottomSpliteLineColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  forState:UIControlStateNormal];
//    [self.bottomBtn setBackgroundImage:[[UIImage lc_imageWithColor:MHTopBottomSpliteLineColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];

   
    [self.nextBtn setBackgroundImage: [self imageByApplyingAlpha:0.5 image:[UIImage imageNamed:@"circle_create_pay"]]      forState:UIControlStateDisabled];
    
}
- (IBAction)nextStepAction:(UIButton *)sender {
    //
        if (!_checkButton.selected) {
            [MBProgressHUD mh_showTips:@"请同意用户协议！"];
            return ;
        }
    if (self.viewModel.isFinished) {
        if (self.viewModel.info.payType == MHCirclContenUnkown) {
            [MBProgressHUD mh_showTips:@"请选择付费模式"];
            return ;
        }
    }else
    {
        if (self.viewModel.info.category == MHCirclContenUnkown) {
            [MBProgressHUD mh_showTips:@"请选择圈子属性"];
            return ;
        }
    }

    [self.viewModel.nextCommand execute:nil];
}


-(UIImage *)imageByApplyingAlpha:(CGFloat )alpha  image:(UIImage*)image
{
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);
    
    CGContextScaleCTM(ctx, 1, -1);
    
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    
    
    CGContextSetAlpha(ctx, alpha);
    
    
    
    CGContextDrawImage(ctx, area, image.CGImage);
    
    
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    
    
    
    UIGraphicsEndImageContext();
    
    
    
    return newImage;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (IBAction)clickVideoType:(UIButton *)sender {
    if (!self.viewModel.isFinished){
        [MBProgressHUD mh_showTips:@"直播圈子暂未开放"];
        return;
    }
    self.topButton.selected = false;
    self.bottomBtn.selected = YES;
    self.nextBtn.enabled = YES;
    if (self.viewModel.isFinished) {
        self.viewModel.info.payType =  MHCirclPayTypeCharge;
    }else
    {
        self.viewModel.info.category =  MHCirclContenTypeVideo;
    }
    
}

- (void)_back{
    self.viewModel.isFinished = false;
    [self.viewModel.services popViewModelAnimated:YES];
//    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)clickContentType:(id)sender {
    self.topButton.selected = YES;
    self.nextBtn.enabled = YES;
    self.bottomBtn.selected = false;
    

    if (self.viewModel.isFinished) {
         self.viewModel.info.payType  =  MHCirclPayTypeFree;
    }else
    {
         self.viewModel.info.category  =  MHCirclContenTypeText;
    }
   
}
- (IBAction)checkAction:(UIButton *) checkButton {
    checkButton.selected = !checkButton.selected;
    self.nextBtn.enabled = checkButton.selected;
}

#pragma mark - Override
- (void)bindViewModel{
    [super bindViewModel];
    BOOL existUser = !self.viewModel.isFinished;
    
    @weakify(self);
    self.metionLable.text = ![AXAppDelegate sharedDelegate].isAuthorizationExpired ? @"设置成功后，圈子属性不能修改":@"设置成功后，付款模式不能修改";
//    [self.topButton setTitle:existUser?@"贴吧圈子":@"FREE 免费" forState:0];
   
    
    if (![AXAppDelegate sharedDelegate].isAuthorizationExpired) {
        self.bottomBtn.hidden = YES;
        [self.topButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.view).offset(-40);
            make.centerX.equalTo(self.view);
        }];
        
        [self.topButton setImage :[UIImage imageNamed:@"circle_createnormal"] forState:0];
        [self.topButton setImage:[UIImage imageNamed:@"circle_createnormalsel"] forState:UIControlStateSelected];
    }
    
    NSString *deviceType = [UIDevice currentDevice].model;
    
    if ([deviceType isEqualToString:@"iPad"] && ![AXAppDelegate sharedDelegate].isAuthorizationExpired) {

        self.nextBtnBottomConstraint.constant = 20;
        [self.topButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.view).offset(-40);
            make.height.mas_equalTo(@60);
            make.centerX.equalTo(self.view);
        }];
    }
//    [self.bottomBtn setTitle:existUser?@"直播圈子":@"PAY 付费" forState:0];
}




-(void) masLayoutSubViews
{
    [self.protocolLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.checkButton.mas_right);
        make.right.equalTo(self.view).offset(-20);
        make.centerY.equalTo(self.checkButton.mas_centerY);
//        make.bottom.eq
        
    }];
}
-(YYLabel *)protocolLable
{
    if(!_protocolLable)
    {
        _protocolLable = [YYLabel new];
        _protocolLable.textAlignment = NSTextAlignmentCenter;
        _protocolLable.textVerticalAlignment = YYTextVerticalAlignmentCenter;
        [_protocolLable sizeToFit];
        _protocolLable.numberOfLines = 0;
        CGFloat  preferWith =   MH_SCREEN_WIDTH -   CGRectGetMaxX(self.checkButton.frame) - 20;
        _protocolLable.preferredMaxLayoutWidth = preferWith;
        //        _protocolLable.lineBreakMode =  yylinb
        //        _protocolLable.backgroundColor = [UIColor colorFromHexString:@"#D9D8D9"];
        NSString *agreeStr =  @"我已经阅读并同意《百唠圈子规则》";
        NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:agreeStr];
        //        one.yy_underlineStyle = NSUnderlineStyleSingle;
        one.yy_font =  MHRegularFont_12;
        one.yy_color = [UIColor colorFromHexString:@"#8e8e8e"];
        one.yy_lineSpacing = 5;
        
//        if (!self.viewModel.isFinished) {
            [one yy_setColor:[UIColor colorFromHexString:@"#ff6946"] range:NSMakeRange(8, 9)];
            
//        }
        NSMutableAttributedString *text = [NSMutableAttributedString new];
        [text appendAttributedString:one];
        //        [text appendAttributedString:[self padding]];
        [self.view addSubview:_protocolLable];
        _protocolLable.attributedText = text;
        @weakify(self);
        _protocolLable.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            YYCache *cache=[YYCache cacheWithName:MHCacheGlobleKey];
            @strongify(self);
            NSArray *h5Links  =  [cache objectForKey:MHH5LinksKey];
            for (MHH5Links *link in h5Links) {
                if ([link.type isEqualToString:@"qzgz"]) {
                    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:link.url]];
                    MHWebViewModel * webViewModel = [[MHWebViewModel alloc] initWithServices:self.viewModel.services params:@{MHViewModelRequestKey:request}];
                    [self.viewModel.services pushViewModel:webViewModel animated:YES];
                    return ;
                }
            }
        };
//        _protocolLable.hidden = self.viewModel.isFinished;
//        self.checkButton.hidden = self.viewModel.isFinished;
    }
    return _protocolLable;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
